<?php
class product_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($ProductID)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_product` 
            WHERE `ProductID`='$ProductID'
        ");
        return $query->result();
    }
    function getByAlias($Alias)
    {
        $Alias=  mysql_escape_string($Alias);
        $array = array("Delete"=>null);
        $this->db->where("(`Alias`='$Alias' or `ProductID`='$Alias')");
        $this->db->where("(`Status`='true' or `Status`='Public')");
        $query = $this->db->get("dtb_product");
        return $query->result();
                        
//        $vlows=array("'","\\","#");
//        $Alias=  str_replace($vlows, "", $Alias);
//        
//        $query = $this->db->query("
//            SELECT * 
//            FROM `dtb_product` 
//            WHERE `Alias`='$Alias'
//        ");
//        return $query->result();
    }
    function gets($begin,$num,$category="",$today=false)
    {
        $re=  isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
        $category= mysql_escape_string($category);
        
        $query = $this->db->query("
            SELECT `dtb_product`.* 
            FROM `dtb_product` 
                ".($category!=""?"
                    INNER JOIN dtb_category 
                    ON (`dtb_product`.Categorys like concat('%--',dtb_category.`id`,'--%') 
                        AND dtb_category.`delete` is null)":"")."
            WHERE `dtb_product`.`Delete` is null
                ".($category!=""?"AND dtb_category.alias='$category'":"")."
                ".($today?" AND DATE_SUB(CURDATE(),INTERVAL 28 DAY) <= dtb_product.StartDate":"")."
                AND NOW() BETWEEN dtb_product.StartDate AND dtb_product.EndDate
                AND (`dtb_product`.`Status`='Public' or `dtb_product`.`Status`='true')
                AND `dtb_product`.`Delete` is null
                ".($re!=""?"AND `dtb_product`.`Regions` like '%--$re--%'":"")."
            ORDER BY `dtb_product`.`StartDate` DESC
            LIMIT $begin,$num
        ");
        return $query->result();
    }
    function counts($category="",$today=false)
    {
        $re=  isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
        $vlows=array("'","\\","#");
        $category=  str_replace($vlows, "", $category);
        
        $query = $this->db->query("
            SELECT count(*) as num 
            FROM `dtb_product` 
                ".($category!=""?"
                    INNER JOIN dtb_category 
                    ON (`dtb_product`.Categorys like concat('%--',dtb_category.`id`,'--%') 
                        AND dtb_category.`delete` is null)":"")."
            WHERE `dtb_product`.`Delete` is null
                ".($category!=""?"AND dtb_category.alias='$category'":"")."
                ".($today?" AND DATE_SUB(CURDATE(),INTERVAL 28 DAY) <= dtb_product.StartDate":"")."
                AND NOW() BETWEEN dtb_product.StartDate AND dtb_product.EndDate
                AND (`dtb_product`.`Status`='Public' or `dtb_product`.`Status`='true')
                AND `dtb_product`.`Delete` is null
                ".($re!=""?"AND `dtb_product`.`Regions` like '%--$re--%'":"")."
            
        ");
        return $query->result();
    }
    function getOtherByTag($ProductID,$tag,$cate="")
    {
        $re=  isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
        $tags=  explode(",", $tag);
        $cates=  explode("--", $cate);
        $SQLQuery="
            SELECT `dtb_product`.* 
            FROM `dtb_product` 
                
            WHERE `dtb_product`.`Delete` is null
                AND NOW() BETWEEN dtb_product.StartDate AND dtb_product.EndDate
                AND (`dtb_product`.`Status`='Public' or `dtb_product`.`Status`='true')
                AND `dtb_product`.`Delete` is null
                AND `ProductID`<>'$ProductID'
                ".($re!=""?"AND `dtb_product`.`Regions` like '%--$re--%'":"")."
                AND ( 
                    
            ";
        if(count($tags)>0)foreach ($tags as $t){
            if($t!=""){
               $SQLQuery.="
                   (CONCAT(',',`dtb_product`.`Tag`,',') like '%,$t,%') OR
                   "; 
            }
        }
        if(count($cates)>0)foreach ($cates as $ca){
            if($ca!=""){
               $SQLQuery.="
                   (`dtb_product`.`Categorys` like '%--$ca--%') OR
                   "; 
            }
        }
        $SQLQuery.="1=2)
            ORDER BY RAND()
            LIMIT 12
            ";
        $query = $this->db->query($SQLQuery);
        
        return $query->result();
    }
}
?>
